// 重写数组中的部分方法

const oldArrayProto = Array.prototype

export const newArrayProto = Object.create(oldArrayProto)

let methods = ['push', 'pop', 'shift', 'unshift', 'reverse', 'splice', 'sort']

methods.forEach((m) => {
  newArrayProto[m] = function (...args) {
    // todo
    // 如果是数组新增的值，也要进行观测
    let inserted
    let ob = this.__ob__
    switch (m) {
      case 'push':
      case 'unshift':
        inserted = args
        break
      case 'splice':
        inserted = args.slice(2)
        break
      default:
        break
    }

    if (inserted) {
      ob.observeArray(inserted)
    }

    const result = oldArrayProto[m].call(this, ...args)
    return result
  }
})
